const fs = require('fs-extra')

const defaultPackageConfig = ({ appName, tplType, scripts }) => ({
  name: appName,
  version: '1.0.0',
  tplType,
  scripts: {
    ...scripts,
    'dl:dev': 'dl dev',
    'dl:build': 'dl build'
  }
})

function packjsonResolver(appName, distPath, { tplType }) {
  const packagePath = distPath + '/package.json'
  const packageText = fs.readFileSync(packagePath, { encoding: 'utf-8' })
  let packageJson = JSON.parse(packageText)
  Object.assign(packageJson, defaultPackageConfig({ appName, tplType, scripts: packageJson.scripts })) // 填写app名称和初始版本
  fs.writeFileSync(packagePath, JSON.stringify(packageJson, null, 2))// 格式转换
}

module.exports = function (...agrs) {
  [packjsonResolver].forEach(fn => fn(...agrs))
}